package com.fastloan.mgt.application.contract.service.impl.loanmode;

import com.fastloan.common.model.CheckResult;
import com.fastloan.mgt.application.base.enums.ApplicationStatusEnum;
import com.fastloan.mgt.application.base.model.Application;
import com.fastloan.mgt.application.base.service.ApplicationService;
import com.fastloan.mgt.application.contract.dao.ApplicationLoanResultMapper;
import com.fastloan.mgt.application.contract.enums.LoanModeEnum;
import com.fastloan.mgt.application.contract.model.ApplicationContract;
import com.fastloan.mgt.application.contract.model.ApplicationLoanResult;
import com.fastloan.mgt.application.contract.service.ApplicationContractService;
import com.fastloan.mgt.application.contract.service.LoanCallback;
import com.fastloan.mgt.application.repay.model.ZyApplicationRepaymentPlan;
import com.fastloan.mgt.application.repay.model.vo.AppCurrRepayExt;
import com.fastloan.mgt.application.repay.service.ZyApplicationRepaymentPlanService;
import com.fastloan.mgt.customer.model.ZyCustomer;
import com.fastloan.mgt.customer.service.ZyCustomerService;
import com.fastloan.mgt.product.basics.model.Product;
import com.fastloan.mgt.product.basics.service.ProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 放款模式
 *
 * @author yanwe
 * createTime 2018-01-2018/1/7 14:00
 */
public abstract class LoanMode {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    protected ProductService productService;

    @Autowired
    protected ApplicationService applicationService;

    @Autowired
    protected ZyCustomerService customerService;

    @Autowired
    protected ApplicationLoanResultMapper applicationLoanResultMapper;

    @Autowired
    protected LoanCallback loanCallback;

    @Autowired
    protected ApplicationContractService applicationContractService;

    @Autowired
    protected ZyApplicationRepaymentPlanService applicationRepaymentPlanService;

    /**
     * 放款
     *
     * @param applicationContract 签约信息
     */
    public abstract CheckResult loan(ApplicationContract applicationContract);


    /**
     * 还款
     *
     * @param applicationId 进件ID
     * @return 结果
     */
    public abstract CheckResult repayment(String applicationId);

    /**
     * 放款回调处理
     *
     * @param applicationLoanResult 进件放款回调结果
     * @return 结果
     */
    protected abstract CheckResult processCallback(ApplicationLoanResult applicationLoanResult);

    /**
     * 放款回调
     *
     * @param applicationLoanResult 进件放款回调结果
     * @return 结果
     */
    public CheckResult loanCallback(ApplicationLoanResult applicationLoanResult) {
        Application application = applicationService.selectByPrimaryKey(applicationLoanResult.getApplicationId());
        ApplicationStatusEnum applicationStatus = ApplicationStatusEnum.getByType(application.getApplicationStatus());
        if(!applicationStatus.equals(ApplicationStatusEnum.APP_LENDING)){
            return CheckResult.fail("该进件状态不能通知放款结果！进件状态:"+applicationStatus.getTypeName());
        }
        return processCallback(applicationLoanResult);
    }

    /**
     * 放款模式
     *
     * @return 放款模式
     */
    public abstract LoanModeEnum loanMode();

    /**
     * 更新放款计划
     *
     * @return 结果
     */
    public abstract CheckResult updateApplicationList(AppCurrRepayExt application);

    protected Product getProduct(String applicationId){
        return productService.selectByPrimaryKey(getApplication(applicationId).getProductId());
    }

    protected Application getApplication(String applicationId){
        return applicationService.selectByPrimaryKey(applicationId);
    }

    protected ZyCustomer getCustomer(String applicationId){
        return customerService.selectByPrimaryKey(getApplication(applicationId).getCustomerId());
    }
}
